<template>
  <!-- 授权报表 -->
  <el-dialog title="选择报表" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="建设项目" prop="projectId">
        <el-select
          v-model="queryParams.projectId"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
          @change="handleQuery"
        >
          <el-option
            v-for="item in projectOptions"
            :key="item.projectId"
            :value="item.projectId"
            :label="item.projectName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报表类型" prop="reportType">
        <el-select v-model="queryParams.reportType" placeholder="请选择报表类型" clearable size="small" @change="handleQuery">
          <el-option
            v-for="dict in dict.type.sys_report_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="reportList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="项目名称" prop="projectName" align="left" :show-overflow-tooltip="true" />
        <el-table-column label="报表名称" prop="reportName" align="left" :show-overflow-tooltip="true" />
        <el-table-column label="报表类型" align="center" prop="reportType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_report_type" :value="scope.row.reportType"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectReport">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { authUserReport, unallocatedUserReportList} from "@/api/project/report";
import { selectProjectList } from "@/api/project/project";

export default {
  dicts: ['sys_report_type'],
  props: {
    // 用户编号
    userId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      reportIds: [],
      // 总条数
      total: 0,
      // 未授权报表数据
      reportList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        reportType: undefined
      },
      // 建设项目选项
      projectOptions: [],
    };
  },
  methods: {
    // 显示弹框
    show() {
      this.resetQuery();
      this.queryParams.userId = this.userId;
      this.getProjectList();
      this.getList();
      this.visible = true;
    },
    /** 建设项目下拉 */
    getProjectList() {
      selectProjectList().then().then(response => {
        this.projectOptions = response.projects;
      })
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.reportIds = selection.map(item => item.reportId);
    },
    // 查询表数据
    getList() {
      unallocatedUserReportList(this.queryParams).then(res => {
        this.reportList = res.rows;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权报表操作 */
    handleSelectReport() {
      const userId = this.queryParams.userId;
      const reportIds = this.reportIds.join(",");
      authUserReport({ userId: userId, reportIds: reportIds }).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      });
    }
  }
};
</script>
